package com.ctgu.rabbitmq.service.impl;

import com.ctgu.rabbitmq.rabbitmq.idempotent.IdempotentConfig;
import com.ctgu.rabbitmq.service.IdempotentService;
import com.ctgu.rabbitmq.utils.ApiResult;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;


/**
 * @ ClassName IdempotentServiceImpl
 * @ Author Alex2
 * @ Date 2025/8/18 14:48
 **/
@Log4j2
@Service
public class IdempotentServiceImpl implements IdempotentService {

    private static final String TAG = "Alex";

    private final RabbitTemplate rabbitTemplate;

    public IdempotentServiceImpl(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public ApiResult idempotent() {
        String message = "Hello World";
//        String messageId = UUID.randomUUID().toString();
        String messageId = "8cdc3efd-500b-4c4c-a3ba-25af9a5b2fd3";
        rabbitTemplate.convertAndSend(
                IdempotentConfig.IDEMPOTENT_EXCHANGE,
                IdempotentConfig.IDEMPOTENT_ROUTING_KEY,
                message,
                msg -> {
                    msg.getMessageProperties().setHeader("messageId", messageId);
                    return msg;
                });
        log.info("{} : 发送消息, messageId={}", TAG, messageId);
        System.out.println(": " + message + ", messageId=" + messageId);
        return ApiResult.success("发送消息成功", message);
    }
}